<?php

/*
 * 文件缓存
 */
class cache
{
    /* 返回缓存 失败false */
    public function GET_Cache($key)
    {
        /* 获取修改时间戳 */
        $fileroot=$_SERVER['DOCUMENT_ROOT'].$GLOBALS['C']['CashRoot'];
        if(!file_exists($fileroot)){mkdir($fileroot);}
        $filename =$fileroot. $key . '.txt';
        $a = filemtime($filename);
        /* 获取时间内容 */
        $content = file_get_contents($filename);
        $array = json_decode($content, true);
        $time = $array['saveTime'];
        if ((time() - $a) > $time) {
            @unlink($filename);
            return false;
        } else {
            return $array;
        }
    }

    public function SAVE_Cache($key, $arr, $time)
    {
        $fileroot=$_SERVER['DOCUMENT_ROOT'].$GLOBALS['C']['CashRoot'];
        if(!file_exists($fileroot)){mkdir($fileroot);}
        $filename = $fileroot. $key . '.txt';
        $arr['saveTime'] = $time;
        $arr = json_encode($arr);
        $bool = file_put_contents($filename, $arr);
        $bool ? 1 : 0;
        return $bool;
    }
}

?>